﻿namespace PugLib.UnitTest
{
	using System;
	using System.Collections.Generic;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	using PugLib.Functional;

	/// <summary>
	///This is a test class for MapReduceMasterTest and is intended
	///to contain all MapReduceMasterTest Unit Tests
	///</summary>
	[TestClass]
	public class MapReduceMasterTest
	{
		[TestMethod]
		public void TestCountItems()
		{
			MapReduceMaster<String, String, String, Int32> master = new MapReduceMaster<String, String, String, Int32>(Map, Reduce);
			Dictionary<String, String> inputData = new Dictionary<String, String>();
			inputData.Add("logFile1.txt", "Item1\nItem2\nItem3\nItem2");
			inputData.Add("logFile2.txt", "Item2\nItem1\nItem1\nItem2");
			IDictionary<String, IList<Int32>> outputData = master.Execute(inputData);
			Assert.AreEqual(3, outputData["Item1"][0]);
			Assert.AreEqual(4, outputData["Item2"][0]);
			Assert.AreEqual(1, outputData["Item3"][0]);
		}

		public static void Map(String key, String value, MapReduceList<String, Int32> result)
		{
			String[] items = value.Split('\n');
			foreach (String item in items)
			{
				result.Add(item, 1);
			}
		}

		public static void Reduce(String key, IList<Int32> values, IList<Int32> result)
		{
			result.Add(0);
			foreach (Int32 value in values)
			{
				result[0] += value;
			}
		}
	}
}